package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.pudao.PuDaoTxLoanBehaviorUsualData;
import com.ucredit.babel.common.model.third.pudao.PuDaoTxLoanBehaviorUsualRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 腾讯风险分层
 *
 * @author guoqiang
 */
public class PuDaoTXLoanBehaviorUsualParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 腾讯风险分层
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        PuDaoTxLoanBehaviorUsualRecord obj = (PuDaoTxLoanBehaviorUsualRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful);
        PuDaoTxLoanBehaviorUsualData scoreData = obj.getData();
        sb.append(formatValue(scoreData.getCode())).append(CRT001);
        sb.append(formatValue(scoreData.getMsg())).append(CRT001);
        sb.append(formatValue(scoreData.getSeqNo())).append(CRT001);
        PuDaoTxLoanBehaviorUsualData.TxLoanBehaviorUsualData data = scoreData.getData();
        PuDaoTxLoanBehaviorUsualData.TxLoanBehaviorUsualDataResponse response = data.getResponse();
        sb.append(formatValue(response.getRequestId())).append(CRT001);
        PuDaoTxLoanBehaviorUsualData.TxLoanBehaviorUsualDataResponseData respData = response.getData();
        sb.append(formatValue(respData.getMessage())).append(CRT001);
        sb.append(formatValue(respData.getCodeDesc())).append(CRT001);
        sb.append(formatValue(respData.getPostTime())).append(CRT001);
        sb.append(formatValue(respData.getRiskScore())).append(CRT001);
        sb.append(formatValue(respData.getExtensionOut())).append(CRT001);
        sb.append(formatValue(respData.getFound())).append(CRT001);
        sb.append(formatValue(respData.getIdFound())).append(CRT001);

        PuDaoTxLoanBehaviorUsualData.RiskInfo risk = respData.getRiskInfo();
        if(risk != null) {
            sb.append(formatValue(risk.getRiskCode())).append(CRT001);
        }else{
            sb.append(CRT001);
        }
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_pudaotx_loan_behavior_usual_inc";
    }

    @Override
    public String getType() {
        return RecordType.PD_TX_LOAN_BEHAVIOR_USUAL_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "腾讯风险分层";
    }
}
